package com.jjw.springbootcvs.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jjw.springbootcvs.constant.MessageConstant;
import com.jjw.springbootcvs.mapper.AddressMapper;
import com.jjw.springbootcvs.mapper.SysUserMapper;
import com.jjw.springbootcvs.pojo.dto.SysUserAndSysRoleDTO;
import com.jjw.springbootcvs.pojo.entity.SysUser;
import com.jjw.springbootcvs.pojo.vo.PageUserListVO;
import com.jjw.springbootcvs.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xiaojuan
 * @Date 2024/11/1
 * @Description 用户业务实现
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final SysUserMapper sysUserMapper;

    @Autowired
    private AddressMapper addressMapper;


    /**
     * 用户登录
     * @param userName 用户名
     * @param password 密码
     * @return 集合
     */
    @Override
    public SysUser login(String userName, String password) {
        return sysUserMapper.login(userName,password);
    }

    /**
     * 查询用户列表
     * @param sysUserAndSysRoleDTO 查询条件
     * @return 分页
     */
    @Override
    public PageInfo<PageUserListVO> getAllUserList(SysUserAndSysRoleDTO sysUserAndSysRoleDTO) {
        PageHelper.startPage(sysUserAndSysRoleDTO.getPageIndex()==null?1:sysUserAndSysRoleDTO.getPageIndex(),10);
        List<PageUserListVO> userListVOList = sysUserMapper.getAllUserListPage(sysUserAndSysRoleDTO);
        return new PageInfo<>(userListVOList);
    }

    /**
     * 根据账号查询用户
     * @param code
     * @return
     */
    @Override
    public SysUser getUserByAccount(String code) {

        SysUser user = sysUserMapper.getUesrByAccount(code);
        if (user!=null){
            throw new RuntimeException(MessageConstant.ALREADY_EXISTS);
        }

        return user;
    }

    /**
     * 新增用户
     * @param sysUser
     */
    @Override
    public void insert(SysUser sysUser) {
        try {
            sysUserMapper.insert(sysUser);
        } catch (Exception e) {
            throw new RuntimeException(MessageConstant.ADD_EDIT_FAILED);
        }
    }

    /**
     * 删除用户
     * 删除用户绑定的地址
     * @param id
     */
    @Transactional
    @Override
    public void delete(Long id) {
        addressMapper.deleteByPrimaryKey(id);
        sysUserMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @Override
    public SysUser getUserById(Long id) {
        return sysUserMapper.selectByPrimaryKey(id);
    }

    /**
     * 修改用户
     * @param sysUser
     */
    @Override
    public void update(SysUser sysUser) {
        sysUserMapper.updateByPrimaryKeySelective(sysUser);
    }

    @Override
    public SysUser getUserByPassword(Long id, String oldPassword) {
       SysUser sysUser = sysUserMapper.getByPassword(id,oldPassword);
       if (sysUser == null){
           throw new RuntimeException(MessageConstant.PASSWORD_ERROR);
       }
        return sysUser;
    }

    @Override
    public void updateByPassword(Long id, String newPassword) {
        sysUserMapper.updateByPassword(id,newPassword);
    }
}
